package com.jgp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jgp.common.R;
import com.jgp.dto.DishDto;
import com.jgp.dto.SetmealDto;
import com.jgp.entity.Category;
import com.jgp.entity.Dish;
import com.jgp.entity.Employee;
import com.jgp.entity.Setmeal;
import com.jgp.service.CateGoryService;
import com.jgp.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CateGoryService cateGoryService;



    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name ){
        log.info("page={},pageSize={},name={}",page,pageSize,name);
        //    构造分页构造器
        Page pageInfo1 = new Page(page,pageSize);
        Page<SetmealDto> pageInfo2 = new Page<>();
        //构造条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        //    过滤条件
        queryWrapper.like(Strings.isNotEmpty(name),Setmeal::getName,name);
        //排序条件
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        //    执行查询
        setmealService.page(pageInfo1, queryWrapper);
        // 对象拷贝
        BeanUtils.copyProperties(pageInfo1,pageInfo2,"records");
        List<Setmeal> records=pageInfo1.getRecords();
        List<SetmealDto> list=records.stream().map((item)->{
                    SetmealDto setmealDto = new SetmealDto();
                    BeanUtils.copyProperties(item,setmealDto);
                    Long categoryId = item.getCategoryId();
                    String name2 = cateGoryService.getById(categoryId).getName();
                    setmealDto.setCategoryName(name2);
                    return setmealDto;
                }).collect(Collectors.toList());
        pageInfo2.setRecords(list);
        return R.success(pageInfo2);
    }
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return  R.success("保存成功");
    }
    @PostMapping("/status/{status}")
    public R<String> updateMulStatus(@PathVariable Integer status, Long[] ids){
        List<Long> list = Arrays.asList(ids);
        //构造条件构造器
        LambdaUpdateWrapper<Setmeal> updateWrapper = new LambdaUpdateWrapper<>();
        //添加过滤条件
        updateWrapper.set(Setmeal::getStatus,status).in(Setmeal::getId,list);
        setmealService.update(updateWrapper);

        return R.success("套餐信息修改成功");
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");

    }

    /**
     * 根据条件查询套促数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list1( Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        setmealLambdaQueryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        setmealLambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(setmealLambdaQueryWrapper);
        return R.success(list);


    }



}
